package br.indt.framework.ui.common;

import javax.microedition.lcdui.Graphics;

public abstract class Widget extends Drawable {
	
	protected static final byte ENABLED = 0;
	protected static final byte DISABLED = 1;
	protected byte state = ENABLED;
	protected boolean selected = false;
	protected boolean horizontal = false;
	protected int menuBarPosition = -1;
	protected Object tag = null;

	protected int posY = 0;
	protected int posX = 0;
	
	public int getPosX() {
		return posX;
	}

	public void setPosX(int posX) {
		this.posX = posX;
	}
	
	public int getPosY() {
		return posY;
	}

	public void setPosY(int posY) {
		this.posY = posY;
	}

	public int getMenuBarPosition()
	{
		return menuBarPosition;
	}

	public void setMenuBarPosition(int menuBarPosition)
	{
		this.menuBarPosition = menuBarPosition;
	}

	public boolean isSelected()
	{
		return selected;
	}

	public void setSelected(boolean selected)
	{
		this.selected = selected;
	}

	public Widget(String name)
	{
		super(name);
	}

	protected void paint(Graphics g, int x, int y, int maxWidth, int maxHeight)
	{
		try
		{
			if (backgroundColor >= 0)
			{
				g.setColor(backgroundColor);
				g.fillRect(x, y, maxWidth, getHeight());
			}

			if (backgroundImage != null) 
			{
				g.drawImage(backgroundImage, x, y, Graphics.TOP | Graphics.LEFT);
			}
			
			if ((backgroundImageLeft != null) && (backgroundImageMiddle != null) && (backgroundImageRight != null))
			{
				int i = 0;

				g.drawImage(backgroundImageLeft, x, y, Graphics.TOP | Graphics.LEFT);

				for (i = x + backgroundImageLeft.getWidth(); i <= getWidth() - backgroundImageRight.getWidth(); i++)
				{
					g.drawImage(backgroundImageMiddle, i, y, Graphics.TOP | Graphics.LEFT);				
				}

				g.drawImage(backgroundImageRight, i, y, Graphics.TOP | Graphics.LEFT);
			}
		}
		catch (Exception e) {
			//empty
		}
	}
	
	public void click(int x, int y)
	{
		setPressedDownState(false);
	}
	
	public void longPressed(int x, int y)
	{
		setPressedDownState(false);
	}

	public void disable() 
	{
		state = DISABLED;
	}
	
	public boolean isDisabled() 
	{
		return (state == DISABLED);
	}

	public void enable()
	{
		state = ENABLED;
	}
	
	public boolean isEnabled()
	{
		return (state == ENABLED);
	}
	
	public abstract void pressFire();

	public boolean isHorizontal()
	{
		return horizontal;
	}

	public void setHorizontal(boolean horizontal)
	{
		this.horizontal = horizontal;
	}

	public Object getTag() {
		return tag;
	}

	public void setTag(Object tag) {
		this.tag = tag;
	}
}